package araujoCoronelVillamayor.plantasVsZombies.uiWicket;

import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.AtaqueAppModel;
import araujoCoronelVillamayor.plantasVsZombies.appModelsArena.SesionAppModel;
import araujoCoronelVillamayor.plantasVsZombies.domain.Fila;
import araujoCoronelVillamayor.plantasVsZombies.domain.Jardin;
import araujoCoronelVillamayor.plantasVsZombies.domain.Zombie;
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.EstadoJardinPanel;
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.JardinPage;
import araujoCoronelVillamayor.plantasVsZombies.uiWicket.layouts.LayoutPrincipal;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Extension;
import org.eclipse.xtext.xbase.lib.Functions.Function0;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure0;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.uqbar.commons.model.UserException;
import org.uqbar.wicket.xtend.WicketExtensionFactoryMethods;
import org.uqbar.wicket.xtend.XButton;
import org.uqbar.wicket.xtend.XListView;

@SuppressWarnings("all")
public class AtaquePage extends LayoutPrincipal {
  @Extension
  private WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new Function0<WicketExtensionFactoryMethods>() {
    public WicketExtensionFactoryMethods apply() {
      WicketExtensionFactoryMethods _wicketExtensionFactoryMethods = new WicketExtensionFactoryMethods();
      return _wicketExtensionFactoryMethods;
    }
  }.apply();
  
  private final JardinPage mainPage;
  
  private final Fila filaAAtacar;
  
  private final AtaqueAppModel ataqueAppModel;
  
  public AtaquePage(final Fila filaParametro, final SesionAppModel sesionAppModelParametro, final JardinPage mainPage) {
    super();
    this.mainPage = mainPage;
    AtaqueAppModel _ataqueAppModel = sesionAppModelParametro.getAtaqueAppModel();
    CompoundPropertyModel<AtaqueAppModel> _compoundPropertyModel = new CompoundPropertyModel<AtaqueAppModel>(_ataqueAppModel);
    this.setDefaultModel(_compoundPropertyModel);
    this.filaAAtacar = filaParametro;
    AtaqueAppModel _ataqueAppModel_1 = sesionAppModelParametro.getAtaqueAppModel();
    this.ataqueAppModel = _ataqueAppModel_1;
    Jardin _jardin = sesionAppModelParametro.getJardin();
    ArrayList<Fila> _filas = _jardin.getFilas();
    int _indexOf = _filas.indexOf(filaParametro);
    int _plus = (_indexOf + 1);
    String _string = Integer.valueOf(_plus).toString();
    String _plus_1 = ("Atacar fila " + _string);
    Label _label = new Label("tituloAtaque", _plus_1);
    this._wicketExtensionFactoryMethods.addChild(this, _label);
    CompoundPropertyModel<AtaqueAppModel> _asCompoundModel = this._wicketExtensionFactoryMethods.<AtaqueAppModel>asCompoundModel(this.ataqueAppModel);
    Form<AtaqueAppModel> _form = new Form<AtaqueAppModel>("nuevoAtaqueForm", _asCompoundModel);
    final Form<AtaqueAppModel> atacarForm = _form;
    this.agregarCamposSeleccion(atacarForm);
    this.agregarAcciones(atacarForm);
    this._wicketExtensionFactoryMethods.addChild(this, atacarForm);
    FeedbackPanel _feedbackPanel = new FeedbackPanel("feedbackPanel");
    this._wicketExtensionFactoryMethods.addChild(this, _feedbackPanel);
    EstadoJardinPanel _estadoJardinPanel = new EstadoJardinPanel("panelEstadoJardin", sesionAppModelParametro);
    final EstadoJardinPanel panelEstadoJardin = _estadoJardinPanel;
    this._wicketExtensionFactoryMethods.addChild(this, panelEstadoJardin);
    this.agregarGrillaAtaqueLog();
  }
  
  public void volver() {
    this.mainPage.refreshFilas();
    this.setResponsePage(this.mainPage);
  }
  
  public MarkupContainer agregarCamposSeleccion(final Form<AtaqueAppModel> parent) {
    DropDownChoice<Zombie> _dropDownChoice = new DropDownChoice<Zombie>("zombieSeleccionado");
    final Procedure1<DropDownChoice<Zombie>> _function = new Procedure1<DropDownChoice<Zombie>>() {
      public void apply(final DropDownChoice<Zombie> it) {
        final Function0<List<Zombie>> _function = new Function0<List<Zombie>>() {
          public List<Zombie> apply() {
            List<Zombie> _zombies = AtaquePage.this.ataqueAppModel.getZombies();
            return _zombies;
          }
        };
        LoadableDetachableModel<List<Zombie>> _loadableModel = AtaquePage.this._wicketExtensionFactoryMethods.<List<Zombie>>loadableModel(_function);
        it.setChoices(_loadableModel);
        final Function1<Zombie,String> _function_1 = new Function1<Zombie,String>() {
          public String apply(final Zombie z) {
            String _nombre = z.getNombre();
            return _nombre;
          }
        };
        ChoiceRenderer<Zombie> _choiceRenderer = AtaquePage.this._wicketExtensionFactoryMethods.<Zombie>choiceRenderer(_function_1);
        it.setChoiceRenderer(_choiceRenderer);
      }
    };
    DropDownChoice<Zombie> _doubleArrow = ObjectExtensions.<DropDownChoice<Zombie>>operator_doubleArrow(_dropDownChoice, _function);
    MarkupContainer _addChild = this._wicketExtensionFactoryMethods.addChild(parent, _doubleArrow);
    return _addChild;
  }
  
  public void agregarAcciones(final Form<AtaqueAppModel> parent) {
    XButton _xButton = new XButton("botonAtacar");
    final Procedure1<XButton> _function = new Procedure1<XButton>() {
      public void apply(final XButton it) {
        final Procedure0 _function = new Procedure0() {
          public void apply() {
            AtaquePage.this.atacar();
          }
        };
        it.setOnClick(_function);
      }
    };
    XButton _doubleArrow = ObjectExtensions.<XButton>operator_doubleArrow(_xButton, _function);
    this._wicketExtensionFactoryMethods.addChild(parent, _doubleArrow);
    XButton _xButton_1 = new XButton("volverJardin");
    final Procedure1<XButton> _function_1 = new Procedure1<XButton>() {
      public void apply(final XButton it) {
        final Procedure0 _function = new Procedure0() {
          public void apply() {
            AtaquePage.this.volver();
          }
        };
        it.setOnClick(_function);
      }
    };
    XButton _doubleArrow_1 = ObjectExtensions.<XButton>operator_doubleArrow(_xButton_1, _function_1);
    this._wicketExtensionFactoryMethods.addChild(parent, _doubleArrow_1);
  }
  
  public void atacar() {
    try {
      SesionAppModel _sesionAppModel = this.ataqueAppModel.getSesionAppModel();
      _sesionAppModel.setFilaSeleccionada(this.filaAAtacar);
      Zombie _zombieSeleccionado = this.ataqueAppModel.getZombieSeleccionado();
      this.ataqueAppModel.atacar(this.filaAAtacar, _zombieSeleccionado);
      this.ataqueAppModel.refresh();
    } catch (final Throwable _t) {
      if (_t instanceof UserException) {
        final UserException e = (UserException)_t;
        String _message = e.getMessage();
        this.error(_message);
      } else {
        throw Exceptions.sneakyThrow(_t);
      }
    }
  }
  
  public void agregarGrillaAtaqueLog() {
    XListView<Object> _xListView = new XListView<Object>("resultadosAtaqueLog");
    final XListView<Object> listViewAtaque = _xListView;
    final Procedure1<ListItem<Object>> _function = new Procedure1<ListItem<Object>>() {
      public void apply(final ListItem<Object> item) {
        Object _modelObject = item.getModelObject();
        CompoundPropertyModel<Object> _asCompoundModel = AtaquePage.this._wicketExtensionFactoryMethods.<Object>asCompoundModel(_modelObject);
        item.setModel(_asCompoundModel);
        Label _label = new Label("mensaje");
        AtaquePage.this._wicketExtensionFactoryMethods.addChild(item, _label);
        Label _label_1 = new Label("atacanteStats");
        AtaquePage.this._wicketExtensionFactoryMethods.addChild(item, _label_1);
        Label _label_2 = new Label("defensorStats");
        AtaquePage.this._wicketExtensionFactoryMethods.addChild(item, _label_2);
      }
    };
    listViewAtaque.setPopulateItem(_function);
    this._wicketExtensionFactoryMethods.addChild(this, listViewAtaque);
  }
  
  public AtaqueAppModel getAtaqueAppModel() {
    Object _defaultModelObject = this.getDefaultModelObject();
    return ((AtaqueAppModel) _defaultModelObject);
  }
}
